<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Adsense;
use App\Http\Requests\Message;
use App\Models\Adsenses;
use App\Models\Messages;
use Illuminate\Http\Request;

class MessageController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(){
        return view('admin.news.message');
    }

    /**
     * 发布
     * @return view
     */
    public function publish(){
        return view('admin.news.message_publish');
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd'    => (string)$request->get('kwd',''),
            ];
            $lists = Messages::getList($params);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 更新
     * @param Message $request
     * @return bool
     */
    public function update(Message $request)
    {
        try{
            $data = $request->all();
            if(strstr($data['user_ids'],'-')){
                $data['user_ids'] = explode('-',$data['user_ids']);
            }else{
                $data['user_ids'] = [$data['user_ids']];
            }
            Messages::create($data);
            return $this->writeJson(0,null,'发布消息成功');
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(!is_array($id)){
                $id=[$id];
            }
            //修改软删除 19-7-31
            $data['status'] = -1;
            Messages::whereIn('id',$id)->update($data);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

}
